/**
 * 
 */
package kr.co.insoft.core.service.template.application.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.model.template.application.ApplicationEntityModel;
import kr.co.insoft.core.model.template.application.ApplicationTypeEntityModel;
import kr.co.insoft.core.model.template.application.EventEntityModel;
import kr.co.insoft.core.model.template.script.ScriptEntityModel;
import kr.co.insoft.core.repository.template.application.ApplicationRepository;
import kr.co.insoft.core.repository.template.application.EventRepository;
import kr.co.insoft.core.repository.template.application.ParamRepository;
import kr.co.insoft.core.service.template.application.ApplicationService;
import kr.co.insoft.core.service.template.script.ScriptService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {
	
	@Autowired
	ScriptService scriptService;
	
	@Autowired
	ApplicationRepository applicationRepository;
	
	@Autowired
	EventRepository eventRepository;
	
	@Autowired
	ParamRepository paramRepository;
	
	private static final Logger logger = LoggerFactory
			.getLogger(ApplicationServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.core.service.template.application.ApplicationService#getPagingList
	 */
	@Override
	public PagingModel getPagingList(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());
			List<ApplicationEntityModel> lists = applicationRepository
					.getPagingList(pagingModel);
			int totalCount = applicationRepository.getTotalCount(pagingModel);

			logger.info("{}", pagingModel.getSearchAndCondition());

			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	public boolean isExistApplicationName(ApplicationEntityModel applicationEntityModel) 
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			applicationEntityModel.setAuth((User) auths.getPrincipal());
			return applicationRepository.isExistApplicationName(applicationEntityModel) > 0;
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	public List<ApplicationTypeEntityModel> getTypeList()
			throws ServiceException {
		try {			
			return applicationRepository.getTypeList();
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createApplication(ApplicationEntityModel applicationEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			applicationEntityModel.setAuth((User) auths.getPrincipal());
			/* Application 등록 */
			applicationEntityModel = applicationRepository.createApplication(applicationEntityModel);
			/* 기본 Install Event 등록 */
			ScriptEntityModel scriptEntityModel = 
					scriptService.getScript(applicationEntityModel.getScriptid());
			
			EventEntityModel eventEntityModel = new EventEntityModel();
			eventEntityModel.setApplicationid(applicationEntityModel.getApplicationid());
			eventEntityModel.setAuth((User) auths.getPrincipal());
			eventEntityModel.setEventName(scriptEntityModel.getScriptName());
			eventEntityModel.setScript(scriptEntityModel.getScript());

			eventRepository.createEvent(eventEntityModel);
		} catch(Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public ApplicationEntityModel getApplication(String applicationid)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			ApplicationEntityModel applicationEntityModel = new ApplicationEntityModel();
			applicationEntityModel.setApplicationid(applicationid);
			applicationEntityModel.setAuth((User) auths.getPrincipal());
			return applicationRepository.getApplication(applicationEntityModel);

		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doRemoveApplication(String applicationid)
			throws ServiceException {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("applicationid", applicationid);
			
			applicationRepository.doRemoveApplication(applicationid);
			eventRepository.doRemoveEvent(param);
			paramRepository.doRemoveParam(param);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
}
